﻿using In66.Net.Mongo.Interfaces;
using In66.Net.Share.Controller;
using MongoDB.Driver;
using Test.Api.Models.Mongo;

namespace Test.Api.Controllers.V2
{
    /// <summary>
    /// 主控制器
    /// </summary>
    [ApiVersion("2.0", Deprecated = true)]
    [Microsoft.AspNetCore.Authorization.Authorize(AuthenticationSchemes = BasicDefaults.AuthenticationScheme)]
    public class HomeController : BaseController
    {
        private readonly IMongoRepository<LoginLog> _mongoRepository;

        /// <summary>
        /// HomeController
        /// </summary>
        /// <param name="mongoRepository"></param>
        public HomeController(
            IMongoRepository<LoginLog> mongoRepository
            )
        {
            _mongoRepository = mongoRepository;
        }

        /// <summary>
        /// Mongo创建
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> MongoCreate()
        {
            LoginLog loginLog = new LoginLog()
            {
                CreateTime = DateTime.Now,
                UserName = "test",
            };

            await _mongoRepository.AddAsync(loginLog);

            return Ok();
        }

        /// <summary>
        /// Mongo获取
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> MongoGetAll()
        {
            var res = await _mongoRepository.GetAllAsync();

            return Ok(res);
        }

        /// <summary>
        /// Mongo分页
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> MongoGetPage()
        {
            var filter = new FilterDefinitionBuilder<LoginLog>();

            var res = await _mongoRepository.PagedAsync(1, 4, filter.Where(p => true), (p) => p.Id, false);

            return Ok(res);
        }
    }
}
